package enjoyhiking.util;

import java.io.File;
import java.io.IOException;
import java.io.FileWriter;

import android.os.Environment;
import android.util.Log;

public class SdCard 
{
	private 		String 			mPath 			= null;
	private 		String 			mName 			= null;
	private 		String 			mFullPath 	= null;
	private 		FileWriter 		mWriter 		= null;
	private 		File 			mFile			= null;

	public SdCard(String user_name, String date, String time, String activity)
	{
		mPath = new String("/sdcard/ActiRecog");
		mName = new String(String.format("%s_%s_%s_%s.acti", date, time, user_name, activity));
		mFullPath = mPath + "/" + mName;
		
		open();
	}
	
	public boolean open()
	{
		File root = Environment.getExternalStorageDirectory();
		
		if(root.canWrite())
		{
			File f = new File(mPath);
			
			if(!f.exists() )
			{
				boolean b = f.mkdirs();
				if(b == false )
				{
					Log.i("dd", "dd");
				}
			}
			
			try 
			{
				 f = new File( mFullPath );
				 f.createNewFile();
				 mFile = f;
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
			
			return true;
		}
		
		return false;
	}
	
	public String read()
	{
		String ret = new String();
		
		//do read it..
		
		return ret;
	}
	
	public void append( String str )
	{
		try
		{
			if( mWriter == null)
			{
				mWriter =  new FileWriter(mFile, true );
			}
			
			mWriter.append( str );
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void close()
	{
		try 
		{
			if(mWriter != null)
			{
				mWriter.flush();
				mWriter.close();
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}
